package com.care.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.care.entity.Consult;
import com.care.entity.Doctor;
import com.care.entity.User;
import com.care.service.ConsultServiceImpl;
import com.care.ui.DocotorPage;
import com.sun.mail.imap.protocol.ID;

@Controller
public class ConsultController {
	@Resource
	private ConsultServiceImpl serviceImpl;
	/**
	 * 根据dc查询consult表
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("consultBydc")
	public String consultBydc(HttpServletRequest request,HttpServletResponse response){
		HttpSession session=request.getSession();
		Doctor doctor=(Doctor) session.getAttribute("doctor");
		String number=request.getParameter("pageNum");
		int pageNum=0;
		if (number==null || number.equals("")) {
			pageNum=1;
		}
		else {
			pageNum=Integer.parseInt(number);
		}
		List<Consult> consults=this.serviceImpl.findConsultBydc(doctor, pageNum, 8);
		DocotorPage<Consult> consultPage=new DocotorPage<Consult>(pageNum, 8);
		int count=this.serviceImpl.countCoultBydc(doctor);
		consultPage.setList(consults);
		consultPage.setTotalCountPage(count);
		session.setAttribute("consultPage", consultPage);		
		return "/showConsultByDoctor.jsp";		
	}
	/**
	 * 根据uc查询consult表
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("consultByuc")
	public String consultByuc(HttpServletRequest request,HttpServletResponse response){
		HttpSession session=request.getSession();
		User user=(User) session.getAttribute("user");
		String number=request.getParameter("pageNum");
		int pageNum=0;
		if (number==null || number.equals("")) {
			pageNum=1;
		}
		else {
			pageNum=Integer.parseInt(number);
		}
		List<Consult> consults=this.serviceImpl.findConsultByuc(user, pageNum, 8);
		DocotorPage<Consult> consultUser=new DocotorPage<Consult>(pageNum, 8);
		int count=this.serviceImpl.countCoultByuc(user);
		consultUser.setList(consults);
		consultUser.setTotalCountPage(count);
		session.setAttribute("consultUser", consultUser);
		
		return "/showConsult.jsp";		
	}
	/**
	 * 判断修改的金额
	 * 并更改用户金额
	 * 修改咨询表金额显示
	 * @param request
	 * @param response
	 * @param consult
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("updateConsultMoney")
	public String updateConsult(HttpServletRequest request,HttpServletResponse response,Consult consult) throws IOException{
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		HttpSession session=request.getSession();
		User user=(User) session.getAttribute("user");		
		int id =Integer.parseInt(request.getParameter("id"));				
		if (this.serviceImpl.updateConsult(consult, user,id)) {
			response.getWriter().write("<script> alert('修改成功');window.location='consultByuc'</script>");	
		}	
		else {
			response.getWriter().write("<script> alert('修改失败,金额不足');window.location='consultByuc'</script>");	
		}
		return null;		
	}
	/**
	 * 用户聊天功能
	 * @param request
	 * @param response
	 * @param consult
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("updateConsultUser")
	public String updateConsultUser(HttpServletRequest request,HttpServletResponse response,Consult consult) throws IOException{
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");						
		int id =Integer.parseInt(request.getParameter("id"));
		this.serviceImpl.updateConsultUser(consult, id);
		response.getWriter().write("<script> alert('信息发送成功,等待医生回复');window.location='consultByuc'</script>");	
		return null;		
	}
	/**
	 * 医生聊天功能
	 * @param request
	 * @param response
	 * @param consult
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("updateConsultDoctor")
	public String updateConsultDoctor(HttpServletRequest request,HttpServletResponse response,Consult consult) throws IOException{
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");						
		int id =Integer.parseInt(request.getParameter("id"));
		this.serviceImpl.updateConsultDoctor(consult, id);
		response.getWriter().write("<script> alert('信息发送成功');window.location='consultBydc'</script>");	
		return null;		
	}
	/**
	 * 确诊为病人
	 * @param request
	 * @param response
	 * @param consult
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("updateConsultDoctorMoney")
	public String updateConsultDoctorMoney(HttpServletRequest request,HttpServletResponse response,Consult consult) throws IOException{
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");	
		HttpSession session=request.getSession();
		Doctor doctor=(Doctor) session.getAttribute("doctor");
		int id =Integer.parseInt(request.getParameter("id"));
		this.serviceImpl.updateConsultDoctorMoney(consult, doctor, id);
		response.getWriter().write("<script> alert('确诊成功');window.location='consultBydc'</script>");	
		return null;		
	}
	/**
	 * 诊断无病
	 * @param request
	 * @param response
	 * @param consult
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("updateConsultDoctorPatient")
	public String updateConsultDoctorPatient(HttpServletRequest request,HttpServletResponse response,Consult consult) throws IOException{
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");	
		int id =Integer.parseInt(request.getParameter("id"));
		String email=request.getParameter("email");
		this.serviceImpl.updateConsultDoctorPatient(consult, email, id);
		response.getWriter().write("<script> alert('操作成功');window.location='consultBydc'</script>");	
		return null;
		
	}
	
}
